package com.hlw.hr_man.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hlw.hr_man.Model.AskForLeave;
import com.hlw.hr_man.service.AskForLeaveService;
import com.hlw.hr_man.serviceImpl.AskForLeaveServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @ClassName: SearchAflApplyController
 * @Description: 处理搜索请假申请信息请求
 * @author 唐明
 * @date 2016年9月20日 上午10:17:42
 *
 */
public class SearchAflApplyController extends HttpServlet{

	/**
	 * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
	 */ 
	private static final long serialVersionUID = 1L;
	
	AskForLeaveService aflService = new AskForLeaveServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		//获得搜索类型
		String searchType = req.getParameter("searchType");
		//获得搜索内容
		String searchContent = req.getParameter("searchContent");
		// 获得当前页码
		int page = Integer.parseInt(req.getParameter("page"));
		// 获得每页行数
		int pageSize = Integer.parseInt(req.getParameter("rows"));
		// 计算开始页码
		int start = (page - 1) * pageSize;
		// 获得搜索到的请假申请
		List<AskForLeave> afls = aflService.getAflApplyBySearch(searchType, searchContent, start, pageSize);
		// 获得搜索到的请假申请记录数目
		int afl_count = aflService.getApplyAflCount(searchType, searchContent);
		// 将申请记录以json格式存放
		JSONObject searchAflApply = new JSONObject();
		searchAflApply.put("total", afl_count);
		searchAflApply.put("rows", JSONArray.fromObject(afls));
		// 将数据传回页面
		PrintWriter pw = resp.getWriter();
		pw.write(searchAflApply.toString());
	}
}
